import { useState, useEffect } from 'react';
import { obj_str_trim, is_obj_eq, ck_date, ck_int, ck_empty, loc, is_empty, obj_str, obj_empty, for_obj } from 'sui';

const useKForm = (data, init) => {
    const [value, setValue] = useState(data);
    const [err, setErr] = useState({});

    useEffect(() => {
        if (obj_empty(init)) {
            return;
        }

        if (!obj_empty(value)) {
            return;
        }

        setValue(init);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [init]);

    useEffect(() => {
        if (is_obj_eq(value, data)) {
            return;
        }

        setValue(data);
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [data]);

    const set_value = (k, v) => {
        setValue(old => ({
            ...old,
            [k]: v
        }));
    };

    const clear_all = () => {
        setErr({});
        setValue({});
    };

    const clear = () => {
        setErr({});
    };

    const isErr = (ck) => {
        if (obj_empty(ck)) {
            return false;
        }

        const error = {};

        for_obj(ck, (k, hck) => {
            if (!hck) {
                return;
            }

            if (k === "end" || k === "pend") {
                const p = k === "end" ? "start" : "pstart";

                const e = ck_range_time(value[k], get(p, false));
                if (e) {
                    error[k] = e;
                }

                return;
            }

            const e = hck(value ? value[k] : null);
            if (e) {
                error[k] = e;
            }
        });

        setErr(error);

        if (obj_empty(error)) {
            return false;
        }

        return true;
    };

    const get = (v, t=true) => {
        if (t) {
            return obj_str_trim(value, v);
        }

        if (!value) {
            return null;
        }

        return value[v];
    }

    return {
        err: err,
        value: value,
        set: set_value,
        setValue: setValue,
        clear: clear,
        clear_all: clear_all,
        isErr: isErr,
        get: get,
    };
};

const is_disable = (data, k) => {
    if (obj_str(data, "sn")) {
        return true;
    }

    return false;
};

const ck_range_time = (s, e) => {
    const err = ck_date(s);
    if (err) {
        return err;
    }

    if (s <= e) {
        return loc("l_673");
    }

    return null;
};

const add_ck_col = (ret, col) => {
    if (is_empty(col)) {
        return;
    }

    col.forEach(d => {
        const sn = d.sn;
        if (ret[sn]) {
            return;
        }

        if (d.opt === "Y") {
            ret[sn] = v => null;
            return;
        }

        if (d.tp === "num") {
            ret[sn] = v => ck_int(v);
        } else {
            ret[sn] = v => ck_empty(v);
        }
    });
};

const useDebounce = (hcmd) => {
    const [timer, setTimer] = useState(null);

    const hdone = () => {
        if (timer) {
            clearTimeout(timer);
        }

        setTimer(setTimeout(() => hcmd(), 200));
    };

    useEffect(() => {
        return () => {
            if (timer) {
                clearTimeout(timer);
            }
        };
    }, [timer]);

    return hdone;
};

export {useDebounce, add_ck_col, is_disable, useKForm};
